/**
 * BindWaitingModal
 * @authors 杨兴洲（of2502）
 * @date    2017/5/24 0024 17:35
 * @version 1.0
 * @last-modify  杨兴洲（of2502）
 */

import React, {Component} from 'react';
import {Modal, Button} from 'antd';

class BindWaitingModal extends Component {

    static defaultProps = {
        authUrl: "javascript:;",
        visible: false
    };

    constructor(props) {
        super(props);
        this.state = {
            visible: props.visible
        };
    }

    render() {
        return (
            <Modal
                title="提示"
                visible={this.state.visible}
                onOk={this.goToDashboard}
                onCancel={this.hideModal}
                okText="已成功设置"
                cancelText="授权失败,重试"
                footer={[
                    <Button key="back" size="large" onClick={this.goToAuth}>授权失败,重试</Button>,
                    <Button key="submit" type="primary" size="large" onClick={this.goToDashboard}>
                        已成功设置
                    </Button>,
                ]}
            >
                <p>请在新窗口中完成微信公众号授权 <a href="http://help.qianmi.com/hc/kb/article/1055867/" target="_blank">查看授权教程</a></p>
            </Modal>
        )
    }

    goToAuth = () => {
        window.open(this.props.authUrl, "_blank");
        this.showModal();
    };

    goToDashboard = () => {
        const key = location.hash.split("?")[1];// 增加key.方便控制台获取角色
        location.href = "/#/overview?"+key;
        this.hideModal();
    };

    showModal = () => {
        this.setState({visible: true});
    };

    hideModal = () => {
        this.setState({visible: false});
    }

}

export default BindWaitingModal;